/** 
 * Copyright 2010 Erlacher Felix, Estgfaeller Wolfgang, Ferula Patrick
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package at.socialconference.app;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import at.socialconference.app.connection.ConnectionHandler;
import at.socialconference.app.persistence.Settings;
import at.socialconference.inter.connection.protocol.SocketProtocol;

public class LoginActivity extends Activity implements OnClickListener {
	private EditText username;
	private EditText password;
	private Button loginBtn;

	private static final int ID_DIALOG_LOGIN = 0;
	private static final int ID_ALERT_NOUSERORPW = 1;
	private static final int ID_ALERT_WRONGPW = 2;
	private static final int ID_ALERT_WRONGLOGIN = 3;
	private static final int ID_ALERT_FAILURE = 4;

	private int returnStatus = -1;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		ConnectionHandler.init(this);
		if(Settings.checkLogin(this)){
			Intent intent = new Intent(this, MenuActivity.class);
			startActivity(intent);

			finish();
		}else{
			setContentView(R.layout.login);
			username = (EditText) findViewById(R.id.usernamefield);
			password = (EditText) findViewById(R.id.passwordfield);
			loginBtn = (Button) findViewById(R.id.loginbtn);
//			username.setText("Patrick");
//			password.setText("patrick");
			loginBtn.setOnClickListener(this);
		}

	}


	public void onClick(View v) {
		if(v.equals(loginBtn)){
			final String user = username.getText().toString();
			final String pass = password.getText().toString();

			if(user.length()==0 || pass.length()==0){
				showDialog(ID_ALERT_NOUSERORPW);
			}else{
				showDialog(ID_DIALOG_LOGIN);
				Thread t = new Thread() {
					public void run() {
						returnStatus = ConnectionHandler.checkLogin(user, pass);
						dismissDialog(ID_DIALOG_LOGIN);
					}
				};

				t.start();

				try {
					t.join();

					switch(returnStatus){
					case SocketProtocol.OK:
						Settings.setUsernameAndPassword(this, user, pass);

						Intent intent = new Intent(this, MenuActivity.class);
						startActivity(intent);

						finish();
						break;
					case SocketProtocol.RESP_NO_LOGIN:
						showDialog(ID_ALERT_WRONGLOGIN);
						break;
					case SocketProtocol.RESP_WRONG_INPUT:
					case SocketProtocol.RESP_WRONG_PW:
						showDialog(ID_ALERT_WRONGPW);
						break;
					case -1:
						showDialog(ID_ALERT_FAILURE);
						break;
					}
				} catch (InterruptedException e) {
					e.printStackTrace();
				}

			}
		}
	}




	@Override
	protected Dialog onCreateDialog(int id) {
		if(id == ID_DIALOG_LOGIN){
			ProgressDialog loadingDialog = new ProgressDialog(this);
			loadingDialog.setTitle("Please wait");
			loadingDialog.setMessage("Logging in");
			loadingDialog.setIndeterminate(true);
			loadingDialog.setCancelable(true);
			return loadingDialog;
		}else if(id == ID_ALERT_NOUSERORPW){
			AlertDialog alertDialog = new AlertDialog.Builder(this).create();
			alertDialog.setTitle("Warning");
			alertDialog.setMessage("Please insert Username and Password");
			return alertDialog;
		}else if(id == ID_ALERT_WRONGPW){
			AlertDialog alertDialog = new AlertDialog.Builder(this).create();
			alertDialog.setTitle("Warning");
			alertDialog.setMessage("Wrong Password");
			return alertDialog;
		}else if(id == ID_ALERT_WRONGLOGIN){
			AlertDialog alertDialog = new AlertDialog.Builder(this).create();
			alertDialog.setTitle("Warning");
			alertDialog.setMessage("Wrong Username");
			return alertDialog;
		}else if(id == ID_ALERT_FAILURE){
			AlertDialog alertDialog = new AlertDialog.Builder(this).create();
			alertDialog.setTitle("Warning");
			alertDialog.setMessage("Server error - please try later");
			return alertDialog;
		}
		return super.onCreateDialog(id);
	}
	
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.loginmenu, menu);
		return super.onCreateOptionsMenu(menu);
	}

	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.lm_mServerSettings:

			Intent it = new Intent(this, ServerSettings.class);
			startActivity(it);

			return true;

		default:
			return super.onOptionsItemSelected(item);
		}
	}
}
